﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EmoneyWeiXinEntity;
using System.Data.SqlClient;
using EmoneyWeiXinCommon;
using System.Data;

namespace EmoneyWeiXinDAL
{
    public class ActivityDataDAL
    {
        public static List<ActivityDataEntity> GetDataTop10(string openid, string ActivityName)
        {
            string sql = @"SELECT top 10 * FROM ActivityData WITH(NOLOCK) where AcitivityName=@ActivityName ORDER BY Jifen DESC";
            SqlParameter[] paras = { new SqlParameter("@ActivityName", ActivityName) };
            return SQLHelper.GetDataByColumnsLst<ActivityDataEntity>(Config.EmoneyWeiXin, sql, paras);
        }

        public static ActivityDataEntity GetOne(string openId, string activityName)
        {
            string sql = @"SELECT ActivityData.*,(RedBag.t_Money-GettedMoney) canGetMoney FROM ActivityData 
                           LEFT JOIN REDBAG ON ActivityData.openId=REDBAG.OpenID AND ActivityData.AcitivityName=RedBag.ActivityName
                           WHERE ActivityData.openId=@openId AND ActivityData.AcitivityName=@AcitivityName";
            SqlParameter[] paras = { 
                                     new SqlParameter("@openId",openId),
                                     new SqlParameter("@AcitivityName",activityName),
                                   };
            return SQLHelper.GetDataByColumns<ActivityDataEntity>(Config.EmoneyWeiXin, sql, paras);
        }

        //添加一条活动数据
        public static void InsertActivityData(string openId, int jifen, string activityName, bool IShaveRedBag, double t_money, string nickName, string imgUrl)
        {
            try
            {
                string sql = "proc_ActiveData";
                SqlParameter[] paras =
            {
              new SqlParameter("@headimgUrl",imgUrl),
              new SqlParameter("@nickName",nickName),
              new SqlParameter("@activityName",activityName),
              new SqlParameter("@openId",openId),
              new SqlParameter("@jifen",jifen),
              new SqlParameter("@IShaveRedBag",IShaveRedBag),
              new SqlParameter("@t_money",t_money)
            };
                SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.StoredProcedure, sql, paras);

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 锁住红包的一条记录，防止通过并发多次获取
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="weixinId"></param>
        /// <param name="activityName"></param>
        /// <param name="t_money"></param>
        /// <param name="result"></param>
        public static void LockRedBag(string openId, string activityName, out double t_money, out string result)
        {

            try
            {
                string sql = "proc_LockRedBag";
                SqlParameter p_Money = new SqlParameter("@t_money", System.Data.SqlDbType.Money);
                p_Money.Direction = System.Data.ParameterDirection.Output;
                p_Money.Size = 200;
                SqlParameter p_result = new SqlParameter("@result", System.Data.SqlDbType.VarChar);
                p_result.Direction = System.Data.ParameterDirection.Output;
                p_result.Size = 200;
                SqlParameter[] paras =
            {
              new SqlParameter("@openId",openId),
              new SqlParameter("@ActivityName",activityName),
              p_result,
              p_Money
            };
                SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.StoredProcedure, sql, paras);
                t_money = Convert.ToDouble(p_Money.Value);
                result = Convert.ToString(p_result.Value);
            }
            catch (Exception)
            {

                throw;
            }

        }

        public static void UpadateRedBag2getted(string openId, string activityName, bool isSuccess, double sendmoney)
        {
            string sql = "";
            if (isSuccess)
            {
                sql = "UPDATE RedBag SET [gettedTime]=GETDATE(),isLock=0 ,GettedMoney=GettedMoney+@sendMoney where openId=@openId AND ActivityName=@ActivityName";
            }
            else
            {
                sql = "UPDATE RedBag SET isLock=0 where openId=@openId AND ActivityName=@ActivityName";
            }
            SqlParameter[] paras = 
            {
              new SqlParameter("@openId",openId),
              new SqlParameter("@ActivityName",activityName),
              new SqlParameter("@sendMoney",sendmoney),
            };
            SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, paras);
        }
        /// <summary>
        /// 添加分享链接的点击记录
        /// </summary>
        public static void AddActivityShareUrlRecord(string openId, string fromOpenId, string activityName)
        {
            string sql = "proc_AddActivityShareUrlRecord";
            SqlParameter[] para =
            {
               new SqlParameter("@fromOpenId",fromOpenId),
               new SqlParameter("@openId",openId),
                new SqlParameter("@activityName",activityName)
            };
            SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.StoredProcedure, sql, para);

        }

        public static DataTable SelectTempRedBag()
        {
            string sql = "SELECT * FROM TempRedBag WHERE IsGetted=0 ";
            return SQLHelper.ExecuteGetTable(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null);
        }

        public static int UpdateTempRedBag2IsGetted(string ID, string orderNum)
        {
            string sql = "UPDATE TempRedBag SET IsGetted=1,orderNum='" + orderNum + "' where ID='" + ID + "'";
            return SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, System.Data.CommandType.Text, sql, null);
        }

    }
}
